#include <bits/stdc++.h>
#define LL long long
using namespace std;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;

  for (cin >> T; T; T--) {
    int n;

    cin >> n;

    const int kMaxL = 30;

    vector<int> a(n);
    vector<int> cnt(kMaxL, 0);

    for (int& x : a) {
      cin >> x;
      for (int j = 0; j < kMaxL; j++) {
        if (x & (1 << j)) {
          cnt[j]++;
        }
      }
    }

    LL ans = 0;
    for (int& x : a) {
      LL sum = 0;
      for (int j = 0; j < kMaxL; j++) {
        if (x & (1 << j)) {
          sum += (LL)(n - cnt[j]) << j;
        } else {
          sum += (LL)cnt[j] << j;
        }
      }
      ans = max(ans, sum);
    }
    cout << ans << "\n";
  }
  return 0;
}